#ifndef __IMAGE_H__
#define __IMAGE_H__

#include <stdlib.h>

#include "tga.h"

//////////////////////////////////////////////////////////////////////////
//image class to store decoded pixels                                   //
//////////////////////////////////////////////////////////////////////////

class Image
{
private:
	int width;
	int pitch;
	int height;
	int format;
	int bitsPerChannel;
	void* pixels;
public:
	enum PixelFormat
	{
		FORMAT_UNDEFINED = 0,
		FORMAT_GREYSCALE = 1,
		FORMAT_RGB = 3,
		FORMAT_RGBA = 4,
	};

	static Image* create(int width, int height, int format, int bitsPerChannel);

	inline int getWidth() const {return width;}
	inline int getPitch() const {return pitch;}
	inline int getHeight() const {return height;}
	inline int getFormat() const {return format;}
	inline int getBitsPerChannel() const {return bitsPerChannel;}
	inline void* getPixels() const {return pixels;}

	bool dumpImage(const TCHAR* outFileName) const;

	~Image();
private:
	Image(int width, int height, int format, void* pixels, int bitsPerChannel);
};

Image::Image(int width, int height, int format, void* pixels, int bitsPerChannel)
{
	this->width = width;
	this->height = height;
	this->format = format;
	this->pixels = pixels;
	this->bitsPerChannel = bitsPerChannel;
	pitch = width * format * (bitsPerChannel >> 3);
}

Image::~Image()
{
	delete[] (unsigned char*)pixels;
	width = 0;
	height = 0;
	format = 0;
	pixels = 0;
	bitsPerChannel = 0;
}

Image* Image::create(int width, int height, int format, int bitsPerChannel)
{
	void* pixels = new unsigned char[width * height * format * (bitsPerChannel >> 3)];
	return new Image(width, height, format, pixels, bitsPerChannel);
}

bool Image::dumpImage(const TCHAR* outFileName) const
{
	if(pixels==NULL)
		return false;
	if(bitsPerChannel!=8)
		return false;

	return tgaSave(outFileName, width, height, bitsPerChannel * format, (unsigned char*)pixels);
}


#endif//__IMAGE_H__
